﻿using Microsoft.Extensions.Configuration;
using PeterKottas.DotNetCore.WindowsService;
using System;
using System.ServiceProcess;
using System.Threading.Tasks;

namespace HS.EIP.WatchService
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceRunner<ServiceHost>.Run(config =>
            {
                config.SetName("HS.EIP.WatchService");
                config.SetDescription("湖山电器会议平台服务守护进程");
                config.SetDisplayName("Hushan EIP WatchService");
                var name = config.GetDefaultName();

                config.Service(serviceConfig =>
                {
                    serviceConfig.ServiceFactory((extraArguments, microServiceController) =>
                    {
                        return new ServiceHost();
                    });
                    serviceConfig.OnStart((service, extraArguments) =>
                    {
                        Console.WriteLine("Service {0} started", name);
                        service.Start();
                    });

                    serviceConfig.OnStop(service =>
                    {
                        Console.WriteLine("Service {0} stopped", name);
                        service.Stop();
                    });

                    serviceConfig.OnInstall(service =>
                    {
                        Console.WriteLine("Service {0} installed", name);
                    });

                    serviceConfig.OnUnInstall(service =>
                    {
                        Console.WriteLine("Service {0} uninstalled", name);
                    });

                    serviceConfig.OnPause(service =>
                    {
                        Console.WriteLine("Service {0} paused", name);
                    });

                    serviceConfig.OnContinue(service =>
                    {
                        Console.WriteLine("Service {0} continued", name);
                    });

                    serviceConfig.OnShutdown(service =>
                    {
                        Console.WriteLine("Service {0} shutdown", name);
                    });

                    serviceConfig.OnError(e =>
                    {
                        var type = e.GetType();
                        //LogManager.GetCurrentClassLogger().
                        NLogHelp.ErrorLog("", e);
                        Console.WriteLine("Service {0} errored with exception : {1}", name, e.Message);
                    });
                });
            });
        }
    }
    
}
